/**
 * Jaxson ajax
 *
 * Copyright Remedy Interactive, licensed under Apache version 2 license:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0.html
 *
 * This notice must remain in all java source files.
 */
package org.jaxson.examples;

import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.jaxson.struts.BaseAction;
import org.jaxson.struts.encoder.ResponseContentType;
import org.jaxson.struts.reverseAjax.ReverseAjaxManager;

/**
 * @author Joe Maisel
 */
@Results
(
	value=
	{
		@Result( name="static", value="/examples/HelloWorldStatic.jsp" ),
		@Result( name="static-javascript", value="/examples/HelloWorldJavascriptTag.jsp" ),
		
		@Result( name="ajax", value="/examples/HelloWorldAjax.jsp" ),
		@Result( name="ajax-echo", value="/examples/HelloWorldAjaxEcho.jsp" ),
		
		@Result( name="reverse-ajax-echo", value="/examples/HelloWorldReverseAjaxEcho.jsp" )
	}
)
public class HelloWorldAction extends BaseAction
{
	private String result = "static";
	private boolean sendViaReverseAjax;
	
	public HelloWorldAction()
	{
		this.model = new HelloWorldModel();
		enableResponseType( ResponseContentType.json );
	}
	
	/**
	 * @see org.jaxson.struts.BaseAction#handle()
	 */
	@Override
	public String handle()
	{
		HelloWorldModel model = (HelloWorldModel)this.model;
		
		if( model.getOrigin() == null )
			model.setOrigin( "The Server" );
		
		if( isSendViaReverseAjax() )
		{
			ReverseAjaxManager.sendImmediately( session, model );
		}
		
		setModel( model );
		
		return result;
	}
	
	public String getResult()
	{
		return result;
	}
	
	public void setResult( String result )
	{
		this.result = result;
	}
	
	public boolean isSendViaReverseAjax()
	{
		return sendViaReverseAjax;
	}

	public void setSendViaReverseAjax( boolean sendViaReverseAjax )
	{
		this.sendViaReverseAjax = sendViaReverseAjax;
	}

	private static final long serialVersionUID = 5174318750297646509L;
}
